package com.lh.java.nio;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

public class MyClient {
	SocketChannel sc;

	Selector selector;

	private final int MAX_LENGTH = 1024;

	// private static Charset charset = Charset.forName("US-ASCII");

	private static Charset charset = Charset.forName("US-ASCII");

	private static CharsetDecoder decoder = charset.newDecoder();

	private static CharsetEncoder encoder = charset.newEncoder();

	private ByteBuffer r_buff = ByteBuffer.allocate(MAX_LENGTH);

	public MyClient() {
		try {
			InetSocketAddress addr = new InetSocketAddress(
					InetAddress.getLocalHost(), 26);

			sc = SocketChannel.open();
			sc.configureBlocking(false);
			sc.connect(addr);
			selector = Selector.open();
			sc.register(selector, SelectionKey.OP_CONNECT);
			System.out.println("register(selector, SelectionKey.OP_CONNECT");
			while (true) {
				int n = selector.select(30);
				// System.out.println("elector.select(30)="+n);
				if (n > 0) {
					Set set = selector.selectedKeys();
					Iterator it = set.iterator();
					while (it.hasNext()) {
						System.out.println("it.hasNext()");
						SelectionKey sk = (SelectionKey) it.next();
						it.remove();
						set.clear();
						if (sk.isConnectable()) {

							if (sc.isConnectionPending()) {
								sc.finishConnect();
							}
							System.out.println("4");
							r_buff.clear();
							// sc.read(r_buff);
							String now = new Date().toString();
							sc.write(encoder.encode(CharBuffer.wrap(now
									+ "/r/n")));

							// r_buff.flip();
							// CharBuffer cb = decoder.decode(r_buff);
							// System.out.print(" : " + cb);
							sc.register(selector, SelectionKey.OP_READ);
						}
						if (sk.isReadable()) {
							sc.read(r_buff);
							r_buff.flip();
							CharBuffer cb = decoder.decode(r_buff);
							System.out.print(" : " + cb);
							sc.register(selector, SelectionKey.OP_WRITE);
						}
						if (sk.isWritable()) {
							r_buff.clear();
							// sc.read(r_buff);
							String now = new Date().toString();
							sc.write(encoder.encode(CharBuffer.wrap(now
									+ "/r/n")));

							// r_buff.flip();
							// CharBuffer cb = decoder.decode(r_buff);
							// System.out.print(" : " + cb);
							sc.register(selector, SelectionKey.OP_READ);
						}
					}
				}
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			try {
				sc.close();
			} catch (IOException ioe) {
				System.err.println("finally sth. is wrong with br.readline() ");
			}
		}
	}

	public static void main(String args[]) {
		new MyClient();
	}

	/*
	 * public MyClient() { try { InetSocketAddress addr = new
	 * InetSocketAddress(InetAddress .getLocalHost(), 26);
	 * 
	 * sc = SocketChannel.open(); sc.configureBlocking(false); sc.connect(addr);
	 * if (sc.finishConnect()) {
	 * System.out.println("connection has been established!...");
	 * 
	 * }
	 * 
	 * r_buff.clear(); // sc.read(r_buff); String now = new Date().toString();
	 * sc.write(encoder.encode(CharBuffer.wrap(now + "/r/n")));
	 * 
	 * selector=Selector.open(); SelectionKey skey = sc.register(selector,0 );
	 * skey.interestOps(SelectionKey.OP_CONNECT);
	 * System.out.println("register(selector, SelectionKey.OP_CONNECT"); while
	 * (true) { System.out.println(selector.select()); if
	 * (selector.select(50000) > 0) {
	 * System.out.println("selector.select(30) > 0"); Set set =
	 * selector.selectedKeys(); Iterator it = set.iterator(); if (it.hasNext())
	 * { System.out.println("it.hasNext()"); SelectionKey sk = (SelectionKey)
	 * it.next(); if (sk.isConnectable()) { System.out.println("4");
	 * r_buff.clear(); // sc.read(r_buff); now = new Date().toString();
	 * sc.write(encoder.encode(CharBuffer.wrap(now + "/r/n")));
	 * 
	 * // r_buff.flip(); // CharBuffer cb = decoder.decode(r_buff); //
	 * System.out.print(" : " + cb); sc.register(selector,SelectionKey.OP_READ);
	 * selector.wakeup(); } } } } } catch (IOException ioe) {
	 * System.err.println("sth. is wrong with br.readline() "); } finally { try
	 * { sc.close(); } catch (IOException ioe) {
	 * System.err.println("finally sth. is wrong with br.readline() "); } }
	 */

}
